package com.ls.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.ls.data.service.*;
import com.ls.interPojo.PhoneAttribution;
import com.ls.interPojo.TodayHistory;
import com.ls.pojo.CustInter;
import com.ls.pojo.Inter;
import com.ls.pojo.InterThirdparty;
import com.ls.utils.HttpClientUtils;
import com.ls.utils.Result;
import com.ls.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 段成
 * @date 2020/1/9 - 20:23
 */
@RestController
@RequestMapping(value = "/TodayHistory")
public class TodayHistoryController {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private InterfaceService interfaceService;

    @Autowired
    private CustomerInterfaceService customerInterfaceService;

    @Autowired
    private InterThirdpartyService interThirdpartyService;

    @Autowired
    private TodayHistoryService todayHistoryService;

    @GetMapping("/getData")
    public Result getData(@RequestParam(name = "month",defaultValue = "") String month,@RequestParam(name = "day",defaultValue = "") String day, @RequestParam(name = "appkey",defaultValue = "") String appkey){
        if("".equals(appkey) || null == appkey || "".equals(month) || null == month || "".equals(day) || null == day){
            return new Result(false, StatusCode.PARAMETER_IS_EMPTY,"参数为空");
        }

        try {
            //  通过地址查询接口id
            StringBuffer requestURL = null;
            requestURL = request.getRequestURL();
            Inter inter = interfaceService.getInterfaceByURL(requestURL.toString());
            if(inter == null){
                return new Result(false, StatusCode.ERROR,"接口不存在");
            }

            CustInter custInter = customerInterfaceService.getAppkeyByAppkey(appkey,inter.getId());

            // 为null 则没有这条记录  appkey不存在或错误
            if(custInter == null){
                return new Result(false, StatusCode.ERROR,"appkey不存在或错误");
            }else {
                //查本地
                TodayHistory todayHistory = todayHistoryService.getDataByMonthAndDay(month,day);
                if(todayHistory!=null){
                    System.out.println("此次查询为本地查询");
                    return new Result(true, StatusCode.OK,"本地查询成功",todayHistory,null);
                }else {
                    System.out.println("本地没有则第三方查询  主1从2辅3");
                    //本地没有则第三方查询  主1从2辅3
                    InterThirdparty interThirdparty1 = interThirdpartyService.getListByInterfaceId(inter.getId(),1);
                    //沉淀数据
                    TodayHistory insertData = this.StitchingAddress(interThirdparty1, month,day);


                    return new Result(true, StatusCode.OK,"第三方查询成功",insertData,null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败出现异常");
        }


    }



    private TodayHistory StitchingAddress(InterThirdparty interThirdparty, String month,String day) {
        //请求第三方数据
        StringBuffer reqUrl = new StringBuffer();
        reqUrl.append(interThirdparty.getThirdpartyUrl());
        reqUrl.append("?key=");
        reqUrl.append(interThirdparty.getAppkey());
        reqUrl.append("&v=1.0&month=");
        reqUrl.append(month);
        reqUrl.append("&day=");
        reqUrl.append(day);
        System.out.println(reqUrl.toString());
        JSONObject jsonObject = HttpClientUtils.httpGet(reqUrl.toString());
        Object result = jsonObject.get("result");

        //沉淀数据
        TodayHistory insertData = new TodayHistory();
        insertData.setDay(day);
        insertData.setMonth(month);
        insertData.setResult(result.toString());
        todayHistoryService.insertData(insertData);
        System.out.println(jsonObject.toString());
        return insertData;
    }
}
